package com.ld.admin.util.daliyImage.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ld.admin.util.daliyImage.DaliyImage;
import com.ld.admin.util.daliyImage.model.DaliyImageModel;
import com.ld.admin.util.daliyImage.model.ImageInfo;
import com.ld.shieldsb.common.core.util.JsoupUtil;

/**
 * bing每日一图
 * 
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2020年7月22日 上午8:33:17
 *
 */
public class DaliyImageBing implements DaliyImage {
    public static final String NAME = "Bing"; // bing
    /*n这个是返回数据的条数,返回最新的几条 
    idx 的意思是跟最新图片更新日期相差几天,比如必应官方最新的图片是07月22号,你设置的idx=2,那么就意味着你获取07月20号的照片*/
    /*
         尺寸可以在返回结果中修改，目前已知的尺寸支持：
    1920x1200   1920x1080    1366x768   1280x768   1024x768    800x600    800x480   768x1280  
    720x1280    640x480      480x800    400x240    320x240     240x320 */
    public static final String URL = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n="; //
    public static final String LINK_URL = "https://cn.bing.com";

    @Override
    public DaliyImageModel getImage(int num) {
        String htmlContent = null;
        DaliyImageModel model = null;
        if (num <= 0) {
            num = 1;
        }
        JSONObject jsonObj = JsoupUtil.getRemoteURLJson(URL + num); //
        /*{
          "images": [
            {
              "startdate": "20200721",
              "fullstartdate": "202007211600",
              "enddate": "20200722",
              "url": "/th?id=OHR.RedBlueWildebeest_ZH-CN1024893552_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
              "urlbase": "/th?id=OHR.RedBlueWildebeest_ZH-CN1024893552",
              "copyright": "日落时分正在迁徙的斑纹角马群，肯尼亚马赛马拉野生动物保护区 (© Denis-Huot/Minden Pictures)",
              "copyrightlink": "https://www.bing.com/search?q=%E9%A9%AC%E8%B5%9B%E9%A9%AC%E6%8B%89%E9%87%8E%E7%94%9F%E5%8A%A8%E7%89%A9%E4%BF%9D%E6%8A%A4%E5%8C%BA&form=hpcapt&mkt=zh-cn",
              "title": "",
              "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20200721_RedBlueWildebeest%22&FORM=HPQUIZ",
              "wp": true,
              "hsh": "70c1d9fb9acbcd479b4415ae45cdb114",
              "drk": 1,
              "top": 1,
              "bot": 1,
              "hs": []
            }
          ],
          "tooltips": {
            "loading": "正在加载...",
            "previous": "上一个图像",
            "next": "下一个图像",
            "walle": "此图片不能下载用作壁纸。",
            "walls": "下载今日美图。仅限用作桌面壁纸。"
          }
        }*/
        // 这里的链接还需要加上微软的http://cn.bing.com在前面
        if (jsonObj != null) {
            model = new DaliyImageModel();
            JSONArray dataResults = jsonObj.getJSONArray("images");

            List<ImageInfo> images = new ArrayList<>();
            for (int i = 0; i < dataResults.size(); i++) {
                JSONObject dataResult = dataResults.getJSONObject(i);
                String copyright = dataResult.getString("copyright");
                String picUrl = LINK_URL + dataResult.getString("url");
                htmlContent = "<img src=\"" + picUrl + "\" width='270' >" + "<br/>"; // height='180'
                htmlContent += copyright;

                ImageInfo image = new ImageInfo();
                image.setPicUrl(picUrl); // 图片
                image.setHtmlContent(htmlContent);
                image.setContent(copyright);
                images.add(image);
            }
            model.setImages(images);

            model.setLinkUrl(LINK_URL);
            model.setSource(NAME);
            model.setUrl(URL);
        }
        return model;
    }

}
